package com.health.system.action.system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import com.health.system.javacommon.jquery.JqueryPageHelper;
import com.health.system.javacommon.jquery.ListRange;

import com.health.system.model.system.UserRole;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;
import static com.health.system.javacommon.util.Struts2JsonHelper.*;

import com.health.system.javacommon.base.*;

import com.health.system.model.system.*;
import com.health.system.dao.system.*;
import com.health.system.service.system.impl.*;
import com.health.system.service.system.*;

/**
 * <p>Title: 系统操作员分配置角色管理 Action</p> 
 * <p>用户基本信息表:TG_USER_INFO,角色基本信息表 TG_ROLE_INFO,用户与角色关联表 TG_USER_ROLE_INFO</p>
 * <p>Description: 页面通过Ajax调用Action层,返回JSONO数据</p>
 * Copyright: Copyright (c) 2011-03 
 * Company: 
 * 
 * @author 
 * @version 1.0 
*/

public class UserRoleAction extends BaseStruts2Action {
	
	private UserRoleManager userRoleManager;
	
	/** 通过spring自动注入 */
	public void setUserRoleManager(UserRoleManager manager) {
		this.userRoleManager = manager;
	}	
	
	/**
	 * jqGrid使用列表
	 * @throws IOException
	 */
	public void list() throws IOException{
		PageRequest<Map> pr = JqueryPageHelper.createPageRequestForJquery(getRequest());
		Page page = userRoleManager.findByPageRequest(pr);
		
		List<UserRole> UserRolelist = (List) page.getResult();
		ListRange<UserRole> resultList = new ListRange<UserRole>();
		resultList.setDataRows(UserRolelist);
        resultList.setRecords(page.getTotalCount());
        resultList.setRows(page.getPageSize());
		if(page.getTotalCount()%page.getPageSize()>0){
			resultList.setTotal(page.getTotalCount() / page.getPageSize()+1);
		}else{
			resultList.setTotal(page.getTotalCount() / page.getPageSize());
		}
		resultList.setPage(pr.getPageNumber());
		outJson(resultList);
	}

	/**
	 * UserRole添加
	 * @throws IOException
	 */
	public void save() throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			HttpServletRequest request = getRequest();
			String userRoleJson = request.getParameter("userRoleJson");
			if (null != userRoleJson&&!"".equals(userRoleJson)) {
				JSONObject jsonobject =JSONObject.fromObject( userRoleJson );
				UserRole userRoleobj = (UserRole)JSONObject.toBean(jsonobject, UserRole.class); 
				userRoleManager.save(userRoleobj);
				result.put("success", true);
				result.put("msg", "添加成功!");
			}else{
				result.put("success", false);
				result.put("msg", "添加失败 数据错误!");
			}
		}catch (Exception e){
			result.put("success", false);
			result.put("msg", "添加失败!");
			e.printStackTrace();
		}
		outJson(result);
	}
	
	/**
	 * UserRole修改
	 * @throws IOException
	 */
	public void update() throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
			try{
				HttpServletRequest request = getRequest();
				String userRoleJson = request.getParameter("userRoleJson");
				if (null != userRoleJson&&!"".equals(userRoleJson)) {
					JSONObject jsonobject =JSONObject.fromObject( userRoleJson );
					UserRole userRoleobj = (UserRole)JSONObject.toBean(jsonobject, UserRole.class); 
					userRoleobj.setId(jsonobject.get("id").toString());
					userRoleManager.update(userRoleobj);
					result.put("success", true);
					result.put("msg", "修改成功!");
				}else{
					result.put("success", false);
					result.put("msg", "修改失败 数据错误!");
				}
			}catch (Exception e){
				result.put("success", false);
				result.put("msg", "修改失败!");
				e.printStackTrace();
			}
		outJson(result);
	}
	
	/**
	 * UserRole删除
	 * @throws IOException
	 */
	public void delete() throws IOException{
		String id = getRequest().getParameter("id");
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			if (null != id&&!"".equals(id)){
				userRoleManager.removeById(id);
				result.put("success", true);
				result.put("msg", "删除成功");
			}else{
				result.put("success", false);
				result.put("msg", "删除失败 数据错误!");
			}
		}catch (Exception e){
			result.put("success", false);
			result.put("msg", "删除失败!");
			e.printStackTrace();
		}
		outJson(result);
	}
	
}
